/*---------------------------------------------------------------------------------------------------------------------------------------------------/

AvatarNpc - Class Title - NEROKING.COM

/---------------------------------------------------------------------------------------------------------------------------------------------------*/

package editor.map.elements{
	
	import com.neroking.resource.Download;
	
	import editor.common.Common;
	import editor.map.elements.Npc;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;
	
	import game.config.StaticConfig;
	
	public final class AvatarNpc extends Npc{
		
		// --- Vars ------------------------------------------------------------------------------------------------------------------------------- //
		
		private var NpcBitmap:Bitmap = new Bitmap();
		private var NpcVector:Vector.<BitmapData> = new Vector.<BitmapData>();
		
		private var NpcTimer:Timer = new Timer(100,0);
		private var NpcFrame:int = 0;
		//站立点数目 
		private var _pointNum:int = 5;
		public var standPoints:Vector.<Sprite> = new Vector.<Sprite>();
		
		// --- Public Functions ------------------------------------------------------------------------------------------------------------------- //
		
		// 构造函数
		public function AvatarNpc(id:String,x:int,y:int){
			super();
			this.x = x;
			this.y = y;
			_mainContainer.addChild(NpcBitmap);
			for(var i:int = 0; i < _pointNum; i ++)
			{
				var pot:Sprite = new Sprite();
				pot.x = - 100 + i * 50;
				pot.y = 100;
				pot.graphics.beginFill(0x00ffff);
				pot.graphics.drawCircle(0,0,10);
				pot.graphics.beginFill(0xff0000);
				pot.graphics.drawCircle(0,0,2);
				pot.graphics.endFill();
				pot.addEventListener(MouseEvent.MOUSE_DOWN, onPotDown);
				pot.addEventListener(MouseEvent.MOUSE_UP, onPotUp);
				addChild(pot);
				standPoints.push(pot);
			}
			npcId = id;
		}
		
		private function onPotDown(e:MouseEvent):void
		{
			var pot:Sprite = e.target as Sprite;
			pot.startDrag();
		}
		
		private function onPotUp(e:MouseEvent):void
		{
			var pot:Sprite = e.target as Sprite;
			pot.stopDrag();
		}
		
		// --- Private Functions ------------------------------------------------------------------------------------------------------------------ //
		private function LoadComplete_func(loader:Loader):void{
			var TmpClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("NPC") as Class;
			var TmpBitmapData:BitmapData = new TmpClass();
			var TmpRect:Rectangle = new Rectangle(0,0,TmpBitmapData.width/10,TmpBitmapData.height/2);
			var TmpPoint:Point = new Point();
			NpcVector = new Vector.<BitmapData>();
			for(var i:int = 0 ; i < 10 ; i++){
				TmpRect.x = TmpRect.width*i;
				var Tmp:BitmapData = new BitmapData(TmpRect.width,TmpRect.height,true,0);
				Tmp.copyPixels(TmpBitmapData,TmpRect,TmpPoint);
				NpcVector.push(Tmp);
			}
			
			NpcTimer.addEventListener(TimerEvent.TIMER,NpcRun_func);
			NpcTimer.start();
			
			NpcBitmap.x = -int(TmpRect.width/2);
			NpcBitmap.y = -int(TmpRect.height/2);
			
			TmpBitmapData.dispose();
			loader.unloadAndStop();
		}
		
		private function NpcRun_func(evt:TimerEvent):void{
			NpcBitmap.bitmapData = NpcVector[NpcFrame];
			NpcFrame++;
			if(NpcFrame >= 10){
				NpcFrame = 0;
			}
		}
		
		override public function set npcId(value:String):void
		{
			super.npcId = value;
			
			if(int(npcId) > 4000)
				Download.load("https://repo.xd.com/svn/tg.doc/Client/assets/avatar/monster/body"+npcId+".swf",LoadComplete_func,[Download.SELF]);
			else
				Download.load("https://repo.xd.com/svn/tg.doc/Client/assets/avatar/npc/body/"+npcId+".swf",LoadComplete_func,[Download.SELF]);
		}
		
	}
}